<?php

// pasar esto a un .ini....

function write_log ($logtext)
{
	static 	$logFile = 'c:\\log\\IFT.log';
	//static 	$logFile = 'c:\\Program Files\\OSGEO\\MapGuide\\Web\\www\\marGuita\\Log\\marTain.log';
	$callip = $_SERVER['REMOTE_ADDR'];
	date_default_timezone_set('America/Argentina/Buenos_Aires');
	
	$fecha = strftime("%x %X");
	$fp = 0;
	$fp = fopen( $logFile, 'a');
	if ($fp <> false)	{
		fwrite($fp, "<" . $callip . "> " . $fecha . "   " . $logtext . "\n");
		fclose($fp);
	}
	else
		echo "<" . $callip . "> " . $fecha . "   " . $logtext . "\n";
}


function customError($error_level, $error_msg, $error_file, $error_line)
{
	//if ($error_level <> E_WARNING) {
	$error_strlevel = FriendlyErrorType($error_level);
	$strlog = "\n<b>*************************\nError:</b> [$error_strlevel] [$error_file:$error_line] : $error_msg <br/>";
	write_log ($strlog);
	if ($error_level <> E_WARNING && $error_level <> E_NOTICE) {
		write_log ("<p><b>Finalizando Sesion<b/><p/>");
		echo "No es posible continuar. Avise administrador revisar archivo de logs con ip: " . 	$_SERVER['REMOTE_ADDR'];
		//alert("No es posible continuar. Revise el archivo de logs");
		die();
	}
	//}
}




function FriendlyErrorType($type)
{
	switch($type)
	{
		case E_ERROR: // 1 //
			return 'E_ERROR';
		case E_WARNING: // 2 //
			return 'E_WARNING';
		case E_PARSE: // 4 //
			return 'E_PARSE';
		case E_NOTICE: // 8 //
			return 'E_NOTICE';
		case E_CORE_ERROR: // 16 //
			return 'E_CORE_ERROR';
		case E_CORE_WARNING: // 32 //
			return 'E_CORE_WARNING';
		case E_CORE_ERROR: // 64 //
			return 'E_COMPILE_ERROR';
		case E_CORE_WARNING: // 128 //
			return 'E_COMPILE_WARNING';
		case E_USER_ERROR: // 256 //
			return 'E_USER_ERROR';
		case E_USER_WARNING: // 512 //
			return 'E_USER_WARNING';
		case E_USER_NOTICE: // 1024 //
			return 'E_USER_NOTICE';
		case E_STRICT: // 2048 //
			return 'E_STRICT';
		case E_RECOVERABLE_ERROR: // 4096 //
			return 'E_RECOVERABLE_ERROR';
		case E_DEPRECATED: // 8192 //
			return 'E_DEPRECATED';
		case E_USER_DEPRECATED: // 16384 //
			return 'E_USER_DEPRECATED';
	}
	return $type;
}

// 
// Funciones para imprimir info de debug
//

// each ReadNext() gets a "row" object, that is,
// an instantiation of a class with properties
// returns number of features in reader
function printFeatureReader($featureReader) {
	global $logFileHandle;
	// no anda!
	static 	$logFile = 'c:\\log\\martain.deb';
	
	$logFileHandle = fopen( $logFile, 'a');

	$i = 0;
	while ($featureReader->ReadNext()) {
		fwrite($logFileHandle, "\nPrinting a feature\n");
		$i++;
		$propCount = $featureReader->GetPropertyCount();
		for($j=0; $j<$propCount; $j++) {
			$propertyName = $featureReader->GetPropertyName($j);
			$boolVal = $featureReader->IsNull($propertyName);
			if ($boolVal) {
				continue;
			}
			$propertyType = $featureReader->GetPropertyType($propertyName);
			printPropertyValueFromFeatReader($featureReader, $propertyType, $propertyName);
		}
	}
	fclose($logFileHandle);
	return $i;
}

function printPropertyValueFromFeatReader($featureReader, $propertyType, $propertyName) {
	global $logFileHandle;
	global $agfReaderWriter;
	global $wktReaderWriter;
	switch ($propertyType) {
		case MgPropertyType::Null :
			fwrite($logFileHandle, "$propertyName is a null property\n");
			break;
		case MgPropertyType::Boolean :
			$val = $featureReader->GetBoolean($propertyName);
			$valStr = printBoolean($val);
			fwrite($logFileHandle, "$propertyName is boolean $valStr \n");
			break;
		case MgPropertyType::Byte :
			$val = $featureReader->GetByte($propertyName);
			fwrite($logFileHandle, "$propertyName has a byte value of $val \n");
			break;
		case MgPropertyType::DateTime :
			$val = $featureReader->GetDateTime($propertyName);
			$valStr = printDateTime($val);
			fwrite($logFileHandle, "DateTime is $valStr\n");
			break;
		case MgPropertyType::Single :
			$val = $featureReader->GetSingle($propertyName);
			fwrite($logFileHandle, "$propertyName is single $val \n");
			break;
		case MgPropertyType::Double :
			$val = $featureReader->GetDouble($propertyName);
			fwrite($logFileHandle, "$propertyName is double $val\n");
			break;
		case MgPropertyType::Int16 :
			$val = $featureReader->GetInt16($propertyName);
			fwrite($logFileHandle, "$propertyName is int16 $val\n");
			break;
		case MgPropertyType::Int32 :
			$val = $featureReader->GetInt32($propertyName);
			fwrite($logFileHandle, "$propertyName is int32 $val\n");
			break;
		case MgPropertyType::Int64 :
			$val = $featureReader->GetInt64($propertyName);
			fwrite($logFileHandle, "$propertyName is int64 $val\n");
			break;
		case MgPropertyType::String :
			$val = $featureReader->GetString($propertyName);
			fwrite($logFileHandle, "$propertyName is string $val\n");
			break;
		case MgPropertyType::Blob :
			fwrite($logFileHandle, "$propertyName is blob\n");
			break;
		case MgPropertyType::Clob :
			fwrite($logFileHandle, "$propertyName is clob\n");
			break;
		case MgPropertyType::Feature :
			$val = $featureReader->GetFeatureObject($propertyName);
			if ($val != NULL) {
				fwrite($logFileHandle, "$propertyName is a feature\n");
				printFeatureReader($val);
			}
			break;
		case MgPropertyType::Geometry :
			fwrite($logFileHandle, "$propertyName is a geometry\n");
			$val = $featureReader->GetGeometry($propertyName);
			if ($val != NULL) {
				$agfReaderWriter = new MgAgfReaderWriter();
				$aGeometry = $agfReaderWriter->Read($val);
				$wktReaderWriter = new MgWktReaderWriter();
				$wktRepresentation = $wktReaderWriter->Write($aGeometry);
				fwrite($logFileHandle, "WKT Representation: $wktRepresentation\n");
			} else {
				fwrite($logFileHandle, "This geometry property is null\n");
			}
			break;
		case MgPropertyType::Raster :
			$val = $featureReader->GetRaster($propertyName);
			fwrite($logFileHandle, "$propertyName is a raster\n");
			break;
		default : fwrite($logFileHandle, "Unknown property type\n");
	}
}


?>